package org.ltc.alliance.service.club;


import org.ltc.alliance.constant.ClubTypeConst;
import org.ltc.alliance.constant.ErrorConst;
import org.ltc.alliance.constant.StatusConst;
import org.ltc.alliance.dao.ClubDao;
import org.ltc.alliance.exception.BusinessException;
import org.ltc.alliance.model.ClubDomain;
import org.ltc.alliance.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class ClubServiceImpl implements ClubService {

    @Autowired
    private ClubDao clubDao;
    @Autowired
    private UserService userService;


    @Override
    public Integer saveClubInfo(ClubDomain club) {

        // 检查社团分类
        if(Objects.isNull(club.getCtype())){
            throw new BusinessException(ErrorConst.Club.CLUB_TYPE_SET_ERROR);
        }

        // 创建社团时状态都是"申请中"
        club.setCstatus(StatusConst.Club.APPLYING);

        // 检查社团创建者是否存在
        userService.userStatusNormalByUid(club.getUid());

        // 检查社团名是否重复
        ClubDomain clubExist = clubDao.getClubByCname(club.getCname());
        if (null != clubExist) {
            throw new BusinessException(ErrorConst.Club.CLUB_NAME_EXIST);
        }
        clubDao.saveClubInfo(club);
        return clubDao.getCidByCname(club.getCname());
    }

    @Override
    public List<ClubDomain> getClubByCtype(ClubTypeConst ctype) {
        return clubDao.getClubByCtype(ctype);
    }

    @Override
    public List<ClubDomain> getClubByCstatus(StatusConst.Club cstatus) {
        return clubDao.getClubByCstatus(cstatus);
    }

    @Override
    public List<ClubDomain> getNormalClubBriefInfo() {
        List<ClubDomain> clubsNormal = clubDao.getClubBriefInfoByCstatus(StatusConst.Club.NORMAL);
        clubsNormal.addAll(clubDao.getClubBriefInfoByCstatus(StatusConst.Club.MODIFIED));
        return clubsNormal;
    }


    @Override
    public StatusConst.Club getCstatusByCname(String cname) {
        return clubDao.getCstatusByCname(cname);
    }

    @Override
    public ClubDomain getClubByCid(Integer cid) {
        this.clubIdExist(cid);;
        return clubDao.getClubByCid(cid);

    }

    @Override
    public void alterCstatusByCname(String cname, Boolean pass, StatusConst.Club preCstatus) {
        // 检查社团名是否存在
        this.clubNameExist(cname);

        if (Objects.isNull(pass)) {
            throw new BusinessException(ErrorConst.Club.STATUS_RESULT_LOST);
        }

        StatusConst.Club clubStatus = clubDao.getCstatusByCname(cname);
        // 检查修改对象是否符合要求
        if (!clubStatus.equals(preCstatus)) {
            throw new BusinessException(ErrorConst.Club.STATUS_ALTER_MISMATCH);
        }
        // 根据修改对象原本类型判断修改种类,是对“创建社团申请”还是“修改社团信息申请”进行审核
        if (preCstatus.equals(StatusConst.Club.APPLYING)) {
            clubDao.alterCstatusByCname(cname ,pass ? StatusConst.Club.NORMAL : StatusConst.Club.REJECTED);
        } else if (preCstatus.equals(StatusConst.Club.MODIFIED)) {
            clubDao.alterCstatusByCname(cname , StatusConst.Club.NORMAL);
        } else {
            throw new BusinessException(ErrorConst.Club.STATUS_ALTER_MISMATCH);
        }

    }

    @Override
    public void alterCstatusByCid(Integer cid, Boolean pass, StatusConst.Club preCstatus) {
        // 检查社团名是否存在
        this.clubIdExist(cid);

        if (Objects.isNull(pass)) {
            throw new BusinessException(ErrorConst.Club.STATUS_RESULT_LOST);
        }

        StatusConst.Club clubStatus = clubDao.getCstatusByCid(cid);
        // 检查修改对象是否符合要求
        if (!clubStatus.equals(preCstatus)) {
            throw new BusinessException(ErrorConst.Club.STATUS_ALTER_MISMATCH);
        }
        // 根据修改对象原本类型判断修改种类,是对“创建社团申请”还是“修改社团信息申请”进行审核
        if (preCstatus.equals(StatusConst.Club.APPLYING)) {
            clubDao.alterCstatusByCid(cid ,pass ? StatusConst.Club.NORMAL : StatusConst.Club.REJECTED);
        } else if (preCstatus.equals(StatusConst.Club.MODIFIED)) {
            clubDao.alterCstatusByCid(cid , StatusConst.Club.NORMAL);
        } else {
            throw new BusinessException(ErrorConst.Club.STATUS_ALTER_MISMATCH);
        }
    }

    @Override
    public void clubStatusNormalByCid(Integer cid) {
        // 检查社团是否存在
        ClubDomain clubExist = clubDao.getClubByCid(cid);
        if (clubExist == null) {
            throw new BusinessException(ErrorConst.Club.CLUB_NOT_EXIST);
        }
        // 检查社团状态是否正常
        if (!clubExist.getCstatus().equals(StatusConst.Club.NORMAL)
                && !clubExist.getCstatus().equals(StatusConst.Club.MODIFIED)) {
            throw new BusinessException(ErrorConst.Club.STATUS_NOT_NORMAL_OR_MODIFIED);
        }
    }

    @Override
    public void clubNameExist(String name) {
        ClubDomain clubExist = clubDao.getClubByCname(name);
        if (null == clubExist) {
            throw new BusinessException(ErrorConst.Club.CLUB_NAME_NOT_EXIST);
        }
    }

    @Override
    public void clubIdExist(Integer cid) {
        ClubDomain clubExist = clubDao.getClubByCid(cid);
        if (null == clubExist) {
            throw new BusinessException(ErrorConst.Club.CLUB_NOT_EXIST);
        }
    }
}
